package com.molyfun.parents.fragment.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class ChoosePayDialogFragment extends DialogFragment implements View.OnClickListener{

    @InjectView(R.id.pay_weixin_layout)
    LinearLayout mWeixinLayout;
    @InjectView(R.id.pay_ali_layout)
    LinearLayout mAliLayout;
    @InjectView(R.id.pay_amount)
    TextView mPayAmount;
    @InjectView(R.id.pay_weixin_checkbox)
    ImageView mWeixinCheckbox;
    @InjectView(R.id.pay_ali_checkbox)
    ImageView mAliCheckbox;
    @InjectView(R.id.to_pay)
    TextView mToPay;

    private int mCurrentPayMethod = PayMethod.PAY_WEIXIN;

    public interface PayMethod{
        int PAY_WEIXIN = 1001;
        int PAY_ALI = 1002;
    }

    private static ChoosePayDialogFragment newInstance(String amount) {
        ChoosePayDialogFragment mStudentMenuDialogFragment = new ChoosePayDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("amount", amount);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_choose_pay, container, false);
        ButterKnife.inject(this, view);
        mPayAmount.setText(getArguments().getString("amount"));
        view.findViewById(R.id.close).setOnClickListener(this);
        mWeixinLayout.setOnClickListener(this);
        mAliLayout.setOnClickListener(this);
        mToPay.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                getDialog().dismiss();
                break;
            case R.id.pay_weixin_layout:
                mCurrentPayMethod = PayMethod.PAY_WEIXIN;
                mWeixinCheckbox.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                mAliCheckbox.setImageResource(R.mipmap.ic_pay_checkbox_normal);
                break;
            case R.id.pay_ali_layout:
                mCurrentPayMethod = PayMethod.PAY_ALI;
                mWeixinCheckbox.setImageResource(R.mipmap.ic_pay_checkbox_normal);
                mAliCheckbox.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                break;
            case R.id.to_pay:
                if(mChoosePayCallback != null){
                    mChoosePayCallback.onChoosePay(mCurrentPayMethod);
                }
                getDialog().dismiss();
                break;
            default:
                break;
        }

    }

    private ChoosePayCallback mChoosePayCallback;

    public interface ChoosePayCallback{
        void onChoosePay(int payMethod);
    }

    public void setChoosePayCallback(ChoosePayCallback choosePayCallback){
        this.mChoosePayCallback = choosePayCallback;
    }

    public static class ChoosePayDialogBuilder {

        ChoosePayDialogFragment mCommonDialogFragment;
        private String mTag;

        public ChoosePayDialogBuilder(String amount){
            mCommonDialogFragment = ChoosePayDialogFragment.newInstance(amount);
            mTag = "choose_pay";
        }

        public ChoosePayDialogBuilder setChooseCallback(ChoosePayCallback chooseCallback){
            mCommonDialogFragment.setChoosePayCallback(chooseCallback);
            return this;
        }

        public ChoosePayDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
//            mCommonDialogFragment.show(fragmentTransaction, mTag);
            fragmentTransaction.add(mCommonDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }


    }
}
